package com.lzb.randomturntable.fragment;

import android.graphics.Color;
import android.media.RingtoneManager;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.blankj.utilcode.util.SizeUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;
import com.lzb.basic.utils.SPUtil;
import com.lzb.randomturntable.BaseNoModelFragment;
import com.lzb.randomturntable.Content;
import com.lzb.randomturntable.R;
import com.lzb.randomturntable.activity.AddTurntableActivity;
import com.lzb.randomturntable.activity.FeedbackActivity;
import com.lzb.randomturntable.activity.SettingActivity;
import com.lzb.randomturntable.activity.TurntableListActivity;
import com.lzb.randomturntable.adapter.TextLineToCenterAdapter;
import com.lzb.randomturntable.bean.TurnTableItem;
import com.lzb.randomturntable.bean.TurntableBean;
import com.lzb.randomturntable.databinding.FragmentHomeBinding;
import com.lzb.randomturntable.dialog.TurntableManageDialog;
import com.lzb.randomturntable.dialog.TurntableSettingDialog;
import com.lzb.randomturntable.utils.AppSpUtils;
import com.lzb.randomturntable.view.TurnTableView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> implements View.OnClickListener {

    private List<TurnTableItem> listData = new ArrayList<>();
    private TextLineToCenterAdapter adapter;


    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSetting.setOnClickListener(this);
        mDataBinding.ivMore.setOnClickListener(this);
        mDataBinding.ivTurntableSetting.setOnClickListener(this);
        mDataBinding.turnTableContainer.setOnClickListener(this);
        mDataBinding.tvRotate.setOnClickListener(this);
        mDataBinding.ivEdit.setOnClickListener(this);

        adapter = new TextLineToCenterAdapter();
        adapter.setTextHOffset(-20);

        mDataBinding.turnTableContainer.setListener(new TurnTableView.IListener() {
            @Override
            public void onRotateStart() {
                //开始转动时的回调
            }

            @Override
            public void onRotateEnd() {
                //转动结束时的回调
            }

            @Override
            public void onSelItem(int pos) {
                //转动结束时，指针所指向的数据位置
                TurnTableItem item = (TurnTableItem) mDataBinding.turnTableContainer.getAdapter().getItem(pos);
                mDataBinding.tvSelect.setText(item.text);
            }
        });
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.ivSetting) {//设置
            startActivity(SettingActivity.class);
        } else if (id == R.id.ivMore) {//更多
            showMoreDialog();
        } else if (id == R.id.ivTurntableSetting) {//转盘设置
            showTurntableSettingDialog();
        } else if (id == R.id.tvRotate || id == R.id.turnTableContainer) {//旋转
            //转动
            mDataBinding.turnTableContainer.startRotate();
        } else if (id == R.id.ivEdit) {//转盘编辑
            List<TurntableBean> listSp = AppSpUtils.getTurntableList();
            if (listSp == null || listSp.size() == 0) {
                AddTurntableActivity.selPosition = -2;
                startActivity(AddTurntableActivity.class);
            } else {
                AddTurntableActivity.selPosition = SPUtil.getInt(mContext, Content.turntablePos, 0);
                startActivity(AddTurntableActivity.class);
            }
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        List<TurntableBean> listSp = AppSpUtils.getTurntableList();
        TurntableBean turntableBean;
        if (listSp == null || listSp.size() == 0) {
            List<String> list = new ArrayList<>();
            list.add("减脂餐");
            list.add("广东菜");
            list.add("重庆火锅");
            list.add("麻辣烫");
            list.add("炸鸡");
            list.add("烧烤");
            turntableBean = new TurntableBean("今晚吃什么", list);
        } else {
            turntableBean = listSp.get(SPUtil.getInt(mContext, Content.turntablePos, 0));
        }
        listData.clear();
        TurnTableItem item;
        for (int i = 0; i < turntableBean.getOptionList().size(); i++) {
            item = new TurnTableItem();
            item.text = turntableBean.getOptionList().get(i);
            item.bgColor = i % 2 == 0 ? Color.parseColor("#FEFEFE") : Color.parseColor("#E5EAFF");
            item.textColor = Color.parseColor("#975E37");
            item.textSize = SizeUtils.sp2px(16);
            listData.add(item);
        }
        mDataBinding.tvProblem.setText(turntableBean.getProblem());
        adapter.setDataList(listData);
        mDataBinding.turnTableContainer.setAdapter(adapter);
    }

    private void showMoreDialog() {
        TurntableManageDialog dialog = new TurntableManageDialog(mContext);
        BasePopupView basePopupView = new XPopup.Builder(mContext).popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                .offsetX(0)//x轴偏移
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(mDataBinding.ivMore).hasShadowBg(false) // 去掉半透明背景
                .asCustom(dialog);
        dialog.setListener(new TurntableManageDialog.Ilistener() {
            @Override
            public void clickAdd() {
                AddTurntableActivity.selPosition = -1;
                startActivity(AddTurntableActivity.class);
            }

            @Override
            public void clickList() {
                startActivity(TurntableListActivity.class);
            }
        });
        basePopupView.show();
    }

    private void showTurntableSettingDialog() {
        TurntableSettingDialog dialog = new TurntableSettingDialog(mContext);
        dialog.show();
    }

}